** Plot Nielsen quarterly, monthly, and BLS CPI 
** JHL  

*************************************
** Set up workspace
*************************************
version 14.0
clear all
set more off

cd "${path_home}"
adopath + ../programs

** log using "${path_log}/a04_PI_plots_cpi_vs_nielsen", text replace

*************************************
** Start work here
*************************************
timer on 1

use "${path_big_dta}/PI/PIq_stores_0615", clear 
foreach v of varlist tvc_t-pl_vul_nsub {
	rename `v' `v'_q
}
cap drop _merge
save "${path_big_dta}/PI/PIq_stores_0615_renamed", replace

*************************************
** [1] Check correlation b/w quarterly and monthly indices 
*************************************

use "${path_big_dta}/PI/PI_stores_0615", clear
gen yq=qofd(dofm(ym))
format yq %tq 
cap drop _merge
	* Not perfect match, number of stores different 
merge m:1 store_code_uc yq using "${path_big_dta}/PI/PIq_stores_0615_renamed", keep(match) nogen

** Something dma codes suddenly became multiple descriptions in 2014-2015, harmonize 
replace dma_descr = "DENVER CO" if dma_code == 751
replace dma_descr = "LOS ANGELES CA" if dma_code == 803
replace dma_descr = "MANKATO MN" if dma_code == 737
replace dma_descr = "MARQUETTE MI" if dma_code == 553
replace dma_descr = "ODESSA-MIDLAND TX" if dma_code == 633
replace dma_descr = "PHOENIX AZ" if dma_code == 753
replace dma_descr = "RENO NV" if dma_code == 811
replace dma_descr = "ROCHESTER NY" if dma_code == 538
replace dma_descr = "SAN FRANCISCO-OAKLAND-SAN JOSE CA" if dma_code == 807
replace dma_descr = "SYRACUSE NY" if dma_code == 555
replace dma_descr = "TULSA OK" if dma_code == 671
replace dma_descr = "WICHITA-HUTCHINSON PLUS KS" if dma_code == 678

* Calculate average inflation rates by store type over entire sample period 
gen pl_tvc_nsub_2015m12 = pl_tvc_nsub if ym == ym(2015,12)
bys channel_code: su pl_tvc_nsub_2015m12, d


* Merge in CPI using DMA and BLS city 
merge m:1 dma_code using "${path_big_dta}/bls/bls_cities_dma", nogen  
merge m:1 area_name year month using "${path_big_dta}/bls/reshape_cpiu_cities_06b", keep(master match) 


** Weighted 
collapse pt_yes_tvc pt_yes_vul pl_tvc_nsub pl_vul_nsub pt_yes_tvc_q pt_yes_vul_q pl_tvc_nsub_q pl_vul_nsub_q valueSAF_i valueSAF1_i valueSAF11_i valueSAH_i valueSAN_i valueSEFV_i valueSEHA_i pl_tvc_nsub_2015m12 ym [aw=tvc_t], by(dma_code dma_descr year month channel_code)

** Plots

*** [3.1] NY, to paper
sort dma_code ym 
local plot 
local j=0 
levelsof dma_descr if dma_code==501, local(dma)
foreach i in D F M {
	local j=`j'+1 
	local j2=`j'+1 
	local plot `plot' (con pl_tvc_nsub ym if dma_code==501&channel_code=="`i'", ///
	legend(lab(`j' "`i', monthly"))) (con pl_tvc_nsub_q ym if dma_code==501&channel_code=="`i'", legend(lab(`j2' "`i', quarterly"))) 
	local j=`j2'
}
tw `plot' (con valueSAF_i ym if dma_code==501, legend(lab(7 "CPI Food and Beverages"))) ///
(con valueSAF11_i ym if dma_code==501, legend(lab(8 "CPI Food at home"))), ytitle(Index value) xtitle(Year-month) title(`dma') ///
graphregion(color(white)) scheme(plotplainblind) legend(pos(6) cols(2))
translate @Graph "${path_out}/graphs/ny_pi_0615.pdf", name("Graph")

** Black and White 
tw `plot' (con valueSAF_i ym if dma_code==501, legend(lab(7 "CPI Food and Beverages"))) ///
(con valueSAF11_i ym if dma_code==501, legend(lab(8 "CPI Food at home"))), ytitle(Index value) xtitle(Year-month) title(`dma') ///
graphregion(color(white)) scheme(plotplain) legend(pos(6) cols(2))
translate @Graph "${path_out}/graphs/ny_pi_0615_bw.pdf", name("Graph")

*************************************
** Close workspace
*************************************
timer off 1
timer list 1
** log close 
